<script setup>
import { getStatistics } from '@/api/project.js';
import * as echarts from 'echarts';
import { onMounted, ref } from 'vue';
import { ElMessage } from 'element-plus';

const statistics = ref({
  projectNum: 0,
  userNum: 0,
  teacherNum: 0,
  orderList: [],
});

const fetchStatistics = async () => {
  try {
    const response = await getStatistics();
    if (response.data.code === 200) {
      statistics.value = response.data.data;
      renderBarChart(); // 渲染排行榜
    } else {
      ElMessage.error(response.msg || '获取统计数据失败！');
    }
  } catch (error) {
    console.error(error);
    ElMessage.error('请求统计数据时发生错误！');
  }
};

const renderBarChart = () => {
  const barChart = echarts.init(document.getElementById('bar-chart'));
  const colors = ['#FFD700', '#C0C0C0', '#CD7F32', '#54A1D3']; // 金、银、铜、普通
  const option = {
    title: {
      text: '项目成员数量排行榜前7',
      left: 'center',
      textStyle: {
        fontSize: 22,
        color: '#00FFFF',
        textShadow: '0px 0px 8px rgba(0, 255, 255, 0.8)',
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 人',
      backgroundColor: 'rgba(0,0,0,0.9)',
      borderColor: '#00FFFF',
      borderWidth: 1,
    },
    xAxis: {
      type: 'value',
      position: 'top',
      axisLine: { show: false },
      axisTick: { show: false },
    },
    yAxis: {
      type: 'category',
      data: statistics.value.orderList.map((item) => item.projectName).reverse(),
      axisLabel: {
        fontSize: 12,
        color: '#FFFFFF',
        textShadow: '0px 0px 6px rgba(255, 255, 255, 0.8)',
      },
      axisLine: { show: false },
      axisTick: { show: false },
    },
    series: [
      {
        type: 'bar',
        data: statistics.value.orderList
            .map((item, index) => ({
              value: item.memberCount,
              itemStyle: { color: colors[index] || colors[3] },
            }))
            .reverse(),
        barWidth: 20,
        label: {
          show: true,
          position: 'right',
          formatter: '{c} 人',
          fontSize: 14,
          color: '#00FFFF',
          textShadow: '0px 0px 6px rgba(0, 255, 255, 0.8)',
        },
      },
    ],
  };
  barChart.setOption(option);
};

onMounted(fetchStatistics);
</script>

<template>
  <div class="statistics-page">
    <h1>项目统计</h1>
    <div class="stat-cards">
      <div class="stat-card">
        <h2>{{ statistics.projectNum }}</h2>
        <p>项目总数</p>
      </div>
      <div class="stat-card">
        <h2>{{ statistics.userNum }}</h2>
        <p>学生总数</p>
      </div>
      <div class="stat-card">
        <h2>{{ statistics.teacherNum }}</h2>
        <p>教师总数</p>
      </div>
      <!-- 新增的两张卡片 -->
      <div class="stat-card">
        <h2>{{ statistics.reviewProject }}</h2>
        <p>待审项目数</p>
      </div>
      <div class="stat-card">
        <h2>{{ statistics.reviewApply }}</h2>
        <p>待审申请数</p>
      </div>
    </div>
    <div id="bar-chart" class="chart-container"></div>
  </div>
</template>


<style scoped>
.statistics-page {
  width: 100%;
  margin: 0 auto;
  padding: 20px;
  background: linear-gradient(135deg, rgba(10, 10, 10, 0.7), rgba(30, 30, 30, 0.9));
  border-radius: 15px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.5);
  position: relative;
  overflow: hidden;
  animation: backgroundAnimation 15s ease-in-out infinite;
}

@keyframes backgroundAnimation {
  0% {
    background: linear-gradient(135deg, rgba(10, 10, 10, 0.7), rgba(30, 30, 30, 0.9));
  }
  50% {
    background: linear-gradient(225deg, rgba(10, 10, 10, 0.7), rgba(20, 30, 30, 0.8));
  }
  100% {
    background: linear-gradient(135deg, rgba(10, 10, 10, 0.7), rgba(30, 30, 30, 0.9));
  }
}

h1 {
  text-align: center;
  color: #00FFFF;
  margin-bottom: 20px;
  text-shadow: 0px 0px 8px rgba(0, 255, 255, 0.8);
  font-family: 'Orbitron', sans-serif;
  position: relative;
  z-index: 1;
}

.stat-cards {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
  padding: 0 20px;
  position: relative;
  z-index: 1;
}

.stat-card {
  flex: 1;
  background: rgba(255, 255, 255, 0.1);
  padding: 20px;
  border-radius: 15px;
  text-align: center;
  box-shadow: 0 4px 15px rgba(0, 255, 255, 0.2);
  margin: 0 10px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  backdrop-filter: blur(10px);
  position: relative;
  z-index: 1;
}

.stat-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 6px 20px rgba(0, 255, 255, 0.4);
  color: #FFFFFF;
  background-color: rgba(0, 0, 0, 0.8);
}

.stat-card h2 {
  font-size: 2.5rem;
  color: #00FFFF;
  margin: 0;
  font-family: 'Orbitron', sans-serif;
}

.stat-card p {
  margin: 5px 0 0;
  color: #FFFFFF;
  text-shadow: 0px 0px 6px rgba(255, 255, 255, 0.8);
}

.chart-container {
  width: 800px;
  height: 400px;
  margin: 20px auto;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(8px);
  padding: 20px;
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.chart-container::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('https://www.transparenttextures.com/patterns/asfalt-dark.png') repeat;
  z-index: -1;
  animation: flowingBackground 10s linear infinite;
}

@keyframes flowingBackground {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(100%);
  }
}

</style>
